
/* Includes ------------------------------------------------------------------*/
#include "stm32_tim.h"


/* TIM状态 */
#if (UART_RECEIVE_FRAME_MODE == USE_TIM_UPDATE_RECEIVE_UART_FRAME)

#if (STM32_LPUART1_ENABLE == 1)
TIM_Status_t TIM_Status_LPUART1_RX;
#endif

#if (STM32_UART1_ENABLE == 1)
TIM_Status_t TIM_Status_UART1_RX;
#endif

#if (STM32_UART2_ENABLE == 1)
TIM_Status_t TIM_Status_UART2_RX;
#endif

#if (STM32_UART3_ENABLE == 1)
TIM_Status_t TIM_Status_UART3_RX;
#endif

#if (STM32_UART4_ENABLE == 1)
TIM_Status_t TIM_Status_UART4_RX;
#endif

#if (STM32_UART5_ENABLE == 1)
TIM_Status_t TIM_Status_UART5_RX;
#endif

#if (STM32_UART6_ENABLE == 1)
TIM_Status_t TIM_Status_UART6_RX;
#endif

#if (STM32_UART7_ENABLE == 1)
TIM_Status_t TIM_Status_UART7_RX;
#endif

#if (STM32_UART8_ENABLE == 1)
TIM_Status_t TIM_Status_UART8_RX;
#endif

#endif



/**
  * @brief  TIM更新中断回调函数
  * @note   无
  * @param  htim: TIM_HandleTypeDef
  * @retval 无
  */
void STM32_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) == SET)
  {
#if UART_RECEIVE_FRAME_MODE == USE_TIM_UPDATE_RECEIVE_UART_FRAME
    
#if (STM32_LPUART1_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_LPUART1_RX)
    {
      if (LPUART1_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(LPUART1_Transfer.RxData, LPUART1_Transfer.DMARxData, LPUART1_Transfer.DMARxData_Len);
        LPUART1_Transfer.RxData_Len = LPUART1_Transfer.DMARxData_Len;
        
        memset(LPUART1_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        LPUART1_Transfer.DMARxData_Len = 0;

        LPUART1_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_LPUART1_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART1_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART1_RX)
    {
      if (UART1_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART1_Transfer.RxData, UART1_Transfer.DMARxData, UART1_Transfer.DMARxData_Len);
        UART1_Transfer.RxData_Len = UART1_Transfer.DMARxData_Len;
        
        memset(UART1_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART1_Transfer.DMARxData_Len = 0;

        UART1_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
    
        /* TIM更新 */
        TIM_Status_UART1_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART2_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART2_RX)
    {
      if (UART2_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART2_Transfer.RxData, UART2_Transfer.DMARxData, UART2_Transfer.DMARxData_Len);
        UART2_Transfer.RxData_Len = UART2_Transfer.DMARxData_Len;
        
        memset(UART2_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART2_Transfer.DMARxData_Len = 0;

        UART2_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_UART2_RX = TIM_STATUS_UPDATE;
    
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART3_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART3_RX)
    {
      if (UART3_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART3_Transfer.RxData, UART3_Transfer.DMARxData, UART3_Transfer.DMARxData_Len);
        UART3_Transfer.RxData_Len = UART3_Transfer.DMARxData_Len;
        
        memset(UART3_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART3_Transfer.DMARxData_Len = 0;

        UART3_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_UART3_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART4_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART4_RX)
    {
      if (UART4_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART4_Transfer.RxData, UART4_Transfer.DMARxData, UART4_Transfer.DMARxData_Len);
        UART4_Transfer.RxData_Len = UART4_Transfer.DMARxData_Len;
        
        memset(UART4_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART4_Transfer.DMARxData_Len = 0;

        UART4_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_UART4_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART5_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART5_RX)
    {
      if (UART5_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART5_Transfer.RxData, UART5_Transfer.DMARxData, UART5_Transfer.DMARxData_Len);
        UART5_Transfer.RxData_Len = UART5_Transfer.DMARxData_Len;
        
        memset(UART5_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART5_Transfer.DMARxData_Len = 0;

        UART5_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_UART5_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART6_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART6_RX)
    {
      if (UART6_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART6_Transfer.RxData, UART6_Transfer.DMARxData, UART6_Transfer.DMARxData_Len);
        UART6_Transfer.RxData_Len = UART6_Transfer.DMARxData_Len;
        
        memset(UART6_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART6_Transfer.DMARxData_Len = 0;

        UART6_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_UART6_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART7_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART7_RX)
    {
      if (UART7_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART7_Transfer.RxData, UART7_Transfer.DMARxData, UART7_Transfer.DMARxData_Len);
        UART7_Transfer.RxData_Len = UART7_Transfer.DMARxData_Len;
        
        memset(UART7_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART7_Transfer.DMARxData_Len = 0;

        UART7_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_UART7_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif

#if (STM32_UART8_ENABLE == 1)
    if (htim->Instance == TIM_PERIPHERAL_UART8_RX)
    {
      if (UART8_Transfer.Transfer_RX_Status == UART_TRANSFER_START)
      {
        memcpy(UART8_Transfer.RxData, UART8_Transfer.DMARxData, UART8_Transfer.DMARxData_Len);
        UART8_Transfer.RxData_Len = UART8_Transfer.DMARxData_Len;
        
        memset(UART8_Transfer.DMARxData, 0, UART_DATALEN_MAX);
        UART8_Transfer.DMARxData_Len = 0;
        
        UART8_Transfer.Transfer_RX_Status = UART_TRANSFER_END;
        
        /* TIM更新 */
        TIM_Status_UART8_RX = TIM_STATUS_UPDATE;
        
        /* 关闭定时器 */
        __HAL_TIM_DISABLE(htim);
      }
    }
#endif  
    
#endif
  }
}


/**
  * @brief  重新初始化TIM
  * @note   无
  * @param  STM32_TIM: TIM_TypeDef
  * @param  htim: TIM_HandleTypeDef
  * @param  psc: 分频系数
  * @param  arr: 重装载值
  * @retval 结果
  */
int32_t STM32_TIM_Init(TIM_TypeDef *STM32_TIM, TIM_HandleTypeDef *htim, uint32_t psc, uint32_t arr)
{
  int32_t res = HAL_OK;

  /* 结构体TIM_HandleTypeDef中的Instance必须指定有硬件定时器，才能使用HAL_TIM_Base_DeInit函数 */
  htim->Instance = STM32_TIM;

  HAL_TIM_Base_DeInit(htim);

  htim->Instance = STM32_TIM;
  htim->Init.Prescaler = psc;
  htim->Init.CounterMode = TIM_COUNTERMODE_UP;
  htim->Init.Period = arr;
  htim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(htim) != HAL_OK)
  {
    res = HAL_ERROR;
  }

  /* 清除更新中断 */
  __HAL_TIM_CLEAR_FLAG(htim, TIM_FLAG_UPDATE);
  __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE);

  /* 打开TIM的更新中断 */
  HAL_TIM_Base_Start_IT(htim);

  /* 关闭定时器 */
  __HAL_TIM_DISABLE(htim);

  return res;
}


